<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>空间分析-核密度分析</title>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" src="../../dist/include-leaflet.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090";
    var map, densityAnalystService, densityAnalystParameters, densityAnalystResult,
        themeService, themeGridRange, themeParameters, themeLayer,
        baseUrl = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";
    map = L.map('map', {
        crs: L.CRS.NonEarthCRS({
            bounds: L.bounds([48.4, -7668.25], [8958.85, -55.58]),
            origin: L.point(48.4, -55.58)
        }),
        center: [-3861.911472192499, 4503.6240321526],
        maxZoom: 18,
        zoom: 2
    });
    L.supermap.tiledMapLayer(baseUrl, {noWrap: true}).addTo(map);
    densityAnalystProcess();

    function densityAnalystProcess() {
        //创建点密度插值分析参数实例
        densityAnalystParameters = new SuperMap.DensityKernelAnalystParameters({
            //指定数据集
            dataset: "Railway@Changchun",
            //指定范围
            bounds: L.bounds([3800, -3800], [8200, -2200]),
            //指定数据集中用于核密度分析的字段
            fieldName: "SmLength",
            searchRadius: 50, //Railway@Changchun的单位是米
            // 结果数据集名称
            resultGridName: "KernelDensity_Result",
            deleteExistResultDataset: true
        });
        //创建一个核密度分析服务实例
        densityAnalystService = L.supermap.spatialAnalystService(serviceUrl);
        densityAnalystService.densityAnalysis(densityAnalystParameters, function (densityServiceResult) {
            densityAnalystResult = densityServiceResult.result;
            if (densityAnalystResult && densityAnalystResult.dataset) {
                //用栅格专题图展示分析结果
                showAnalysisResult_ThemeGridRange();
            } else {
                alert(densityServiceResult.error.errorMsg);
            }
        });
    }

    //构造栅格专题图
    function showAnalysisResult_ThemeGridRange() {
        //创建专题图服务实例
        themeService = L.supermap.themeService(baseUrl);
        var color1 = new SuperMap.ServerColor(255, 212, 170),
            color2 = new SuperMap.ServerColor(255, 127, 0),
            color3 = new SuperMap.ServerColor(191, 95, 0),
            color4 = new SuperMap.ServerColor(255, 0, 0),
            color5 = new SuperMap.ServerColor(191, 0, 0);
        var themeGridRangeItem1 = new SuperMap.ThemeGridRangeItem({
                start: 0,
                end: 0.05,
                color: color1
            }),
            themeGridRangeItem2 = new SuperMap.ThemeGridRangeItem({
                start: 0.05,
                end: 5,
                color: color2
            }),
            themeGridRangeItem3 = new SuperMap.ThemeGridRangeItem({
                start: 5,
                end: 10,
                color: color3
            }),
            themeGridRangeItem4 = new SuperMap.ThemeGridRangeItem({
                start: 10,
                end: 100,
                color: color4
            }),
            themeGridRangeItem5 = new SuperMap.ThemeGridRangeItem({
                start: 100,
                end: 360,
                color: color5
            });
        themeGridRange = new SuperMap.ThemeGridRange({
            reverseColor: false,
            rangeMode: SuperMap.RangeMode.EQUALINTERVAL,
            //栅格分段专题图子项数组
            items: [themeGridRangeItem1,
                themeGridRangeItem2,
                themeGridRangeItem3,
                themeGridRangeItem4,
                themeGridRangeItem5
            ]
        });

        themeParameters = new SuperMap.ThemeParameters({
            //制作专题图的数据集
            datasetNames: [densityAnalystResult.dataset.split('@')[0]],
            dataSourceNames: ["Changchun"],
            joinItems: null,
            themes: [themeGridRange]
        });

        themeService.getThemeInfo(themeParameters, function (getThemeInfoServiceResult) {
            var result = getThemeInfoServiceResult.result;
            if (result && result.newResourceID) {
                themeLayer = L.supermap.tiledMapLayer(baseUrl, {
                    noWrap: true,
                    cacheEnabled: false,
                    transparent: true,
                    layersID: result.newResourceID,
                    opacity: 0.8
                }).addTo(map);
            }
        }).on('failed', function (error) {
            alert(error.errorMsg);
        });
    }
</script>
</body>
</html>